<?php


namespace Biz\model\NiceGoodsTopic;

use Biz\model\advertisement\MBaseDbModel;
use Biz\service\NiceGoodsTopic\Topic;
use Illuminate\Session\FileSessionHandler;

class SecondTopic extends MBaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'nice_goods_sec_topic';

    const STATUS_NORMAL =  1;
    const  STATUS_DELETE =  9;
    public const STATUS_WAIT = 11;
    public const STATUS_END = 13;

    //虚拟状态对应值
    public const STATUS_ARR = [
        self::STATUS_WAIT => "待上线",
        self::STATUS_END => "已上线",
        self::STATUS_DELETE => "下线",
    ];

    public static function getInfo($id)
    {
        $rows = self::select('*', ['id' => $id]);

        return current($rows);
    }


    public static function isDel($id)
    {
        return self::count(['topic_status' => self::STATUS_DELETE, 'id' => $id]);
    }

    public static function offline($id)
    {
        self::update(['topic_status' => self::STATUS_DELETE], ['id' => $id]);
    }

    public static function online($id)
    {
        self::update(['topic_status' => self::STATUS_NORMAL], ['id' => $id]);
    }

    public static function getRelaTopicByPid($pid , $topic_id ,$field = '*')
    {
        return self::select($field , [
            'pid' =>$pid ,
            'id[!]' => $topic_id,
            'topic_status' => self::STATUS_NORMAL ,
            'show_time[<=]' => date("Y-m-d H:i:s"),
            'ORDER' => ['show_time' => 'DESC']
            ]);
    }


    public static function getByWhere($where , $field = '*')
    {
        return self::select($field , $where);
    }


    public static function getHeadTopic()
    {
        $now = date("Y-m-d H:i:s" , time());

        return self::select([
            "[>]" . FirstTopic::_TABLE_NAME => [self::_TABLE_NAME . ".pid" => "id"],
        ],[
            self::_TABLE_NAME . ".id",
            self::_TABLE_NAME . ".pid",
            self::_TABLE_NAME . ".topic_name",
            self::_TABLE_NAME . ".show_time",
            self::_TABLE_NAME . ".desc",
            self::_TABLE_NAME . ".img",
            FirstTopic::_TABLE_NAME . ".name",
        ],[
            self::_TABLE_NAME . ".topic_status" => self::STATUS_NORMAL,
            FirstTopic::_TABLE_NAME . ".topic_status" => FirstTopic::STATUS_NORMAL,
            self::_TABLE_NAME . ".show_time[<=]" => $now,
            'ORDER' => [self::_TABLE_NAME . ".show_time" => 'DESC'],
        ]);
    }



}
